#include <iostream>
#include <pthread.h>
#include <cstdio>
#include <cstring>
#include <unistd.h>

using namespace std;

int tickets = 10000;

void* getTickets(void* args)
{
    const char* name = static_cast<const char*>(args);

    while (true)
    {
        if (tickets > 0)
        {
            usleep(123);
            cout << name << " 抢到了票, 票的编号: " << tickets << endl;
            tickets--;
        }
        else
        {
            // 票抢到几张，就算没有了呢？0
            cout << name << "] 已经放弃抢票了，因为没有了..." << endl;
            break;
        }
    }

    return nullptr;
}

int main()
{
    pthread_t tid1;
    pthread_t tid2;
    pthread_t tid3;

    pthread_create(&tid1, nullptr, getTickets, (void*)"thread1");
    pthread_create(&tid2, nullptr, getTickets, (void*)"thread2");
    pthread_create(&tid3, nullptr, getTickets, (void*)"thread3");

    pthread_join(tid1, nullptr);
    pthread_join(tid2, nullptr);
    pthread_join(tid3, nullptr);

    return 0;
}